package com.witdata.controller.student;

import com.witdata.mysql.base.service.UserLearningSessionService;
import com.witdata.mysql.domain.entity.UserLearningSession;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.witdata.common.constant.RedisConstant.REDISKEYLOGIN;


@Service
@RequiredArgsConstructor
public class TimeTaskController {

    private final UserLearningSessionService userLearningSessionService;
    private final RedisTemplate<String,String> redisTemplate;
    private static final Logger log = LoggerFactory.getLogger(TimeTaskController.class);

    private static final long INACTIVITY_THRESHOLD = 72000000; // 12小时
//    private static final long INACTIVITY_THRESHOLD = 600000; // 10分钟
//    private static final long INACTIVITY_THRESHOLD = 10000; // 10秒
    private HttpServletResponse httpServletResponse;
    /**
     * 每分钟检查一次不活跃用户
     */
//    @Scheduled(fixedRate = 60000)
//    @Scheduled(fixedRate = 60000)
//    @Scheduled(fixedRate = 10000)
    public void checkInactiveUsers() {
//        log.info("定时执行-------");
//        List<UserLearningSession> userActivities = userLearningSessionService.getAllUserActivities(redisTemplate.opsForValue().get(REDISKEYLOGIN+"userName"));
//
//        long now = System.currentTimeMillis();
//        for (UserLearningSession userActivity : userActivities) {
//            if ((now - userActivity.getLastActivityTime()) > INACTIVITY_THRESHOLD) {
//                // 用户不活跃，可以在这里处理不活跃用户
//                log.info("多少秒" + (now - userActivity.getLastActivityTime()));
//                userLearningSessionService.updateStatus(userActivity.getUsername());
//                userLearningSessionService.updateUserActivity(userActivity.getUsername());
//                log.info("User " + userActivity.getUsername() + " is inactive.");
//            }
//        }
    }
}